var gulp = require('gulp');
var sass = require('gulp-sass');
var webpack = require('gulp-webpack');
var ts = require('gulp-typescript');
var clean = require('gulp-clean');

gulp.task('default', function() {
    // 将你的默认的任务代码放在这
});

gulp.task('clean', function() {
    return gulp.src(['./dist', './public/dist/*', './public/app/*', './server-modules/dist/*'], {
            read: false
        })
        .pipe(clean({
            force: true
        }));
});

gulp.task('webpack', ['clean'], function() {
    return gulp.src('./web-src/app/main.ts')
        .pipe(webpack({
            entry: {
                app: './web-src/app/main.ts'
            },
            output: {
                filename: 'bundle.js',
            },
            resolve: {
                extensions: ['', '.js', '.ts'],
            },
            module: {
                loaders: [{
                    test: /\.ts/,
                    loaders: ['ts-loader'],
                    exclude: /node_modules/
                }]
            }
        }))
        .pipe(gulp.dest('./public/dist/'));
});

var tsProject = ts.createProject('tsconfig.json');
gulp.task('tsc-app', ['webpack'], function() {
    return gulp.src('./server-modules/src/app.ts')
        .pipe(ts(tsProject))
        .pipe(gulp.dest('./server-modules/dist'));
})

gulp.task('build', ['tsc-app', 'webpack'], function() {
    return gulp.src(['./web-src/app/*.css', './web-src/app/*.html'])
        .pipe(gulp.dest('./public/app/'));
})

gulp.task('watch', function () {
    gulp.watch(['./server-modules/src/app.ts', './web-src/app/*.ts'], ['build']);
})